<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>

	
		<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
		<meta http-equiv="Content-Language" content="en-us">

		<title>Django | Django shortcut functions | Django Documentation</title>

		<meta name="ROBOTS" content="ALL">
		<meta http-equiv="imagetoolbar" content="no">
		<meta name="MSSmartTagsPreventParsing" content="true">
		<meta name="Copyright" content="This site's design and contents Copyright (c) 2005  Lawrence Journal-World.">

		<meta name="keywords" content="Python, Django, framework, open-source">
		<meta name="description" content="Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.">

		<link href="Django%20%7C%20Django%20shortcut%20functions%20%7C%20Django%20Documentation_files/base.css" rel="stylesheet" type="text/css" media="screen">
		<link href="Django%20%7C%20Django%20shortcut%20functions%20%7C%20Django%20Documentation_files/print.css" rel="stylesheet" type="text/css" media="print">
      
  
  <style type="text/css" media="screen">
    #docs-search {
      color: #000;
      float: right;
    }
    #docs-search form {
      font-size: 92%;
      margin: 0;
      padding: 1em 1em 0;
      white-space: nowrap;
    }
    form.search ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    form.search li {
      display: inline;
      padding-right: 1em;
    }
    form.search .query {
      width: 18em;
    }
  </style>
  <link rel="stylesheet" href="Django%20%7C%20Django%20shortcut%20functions%20%7C%20Django%20Documentation_files/pygments.css" type="text/css">

	</head><body id="documentation" class="default">

	<div id="container">
		<div id="header">
			<h1 id="logo"><a href="http://www.djangoproject.com/"><img src="Django%20%7C%20Django%20shortcut%20functions%20%7C%20Django%20Documentation_files/hdr_logo.gif" alt="Django"></a></h1>
			<ul id="nav-global">
				<li id="nav-homepage"><a href="http://www.djangoproject.com/">Home</a></li>
				<li id="nav-download"><a href="http://www.djangoproject.com/download/">Download</a></li>
				<li id="nav-documentation"><a href="http://docs.djangoproject.com/">Documentation</a></li>
				<li id="nav-weblog"><a href="http://www.djangoproject.com/weblog/">Weblog</a></li>
				<li id="nav-community"><a href="http://www.djangoproject.com/community/">Community</a></li>
				<li id="nav-code"><a href="http://code.djangoproject.com/">Code</a></li>
			</ul>
		</div>
		<!-- END Header -->
		<div id="billboard">
  <h2><a href="http://docs.djangoproject.com/en/1.0/">Django documentation</a></h2>
</div>
		<div id="columnwrap">
			
		<div id="content-main">
		


  <h2 class="deck">
  
    This document describes Django version 1.0. For development documentation, 
    <a href="http://docs.djangoproject.com/en/dev/topics/http/shortcuts/">go here</a>.
  
  </h2>
  <div class="section" id="s-django-shortcut-functions">
<span id="s-topics-http-shortcuts"></span><span id="django-shortcut-functions"></span><span id="topics-http-shortcuts"></span><h1>Django shortcut functions<a class="headerlink" href="#django-shortcut-functions" title="Permalink to this headline">¶</a></h1>
<p>The package <tt class="docutils literal"><span class="pre">django.shortcuts</span></tt> collects helper functions and classes that
“span” multiple levels of MVC. In other words, these functions/classes
introduce controlled coupling for convenience’s sake.</p>
<div class="section" id="s-render-to-response">
<span id="render-to-response"></span><h2><tt class="docutils literal"><span class="pre">render_to_response()</span></tt><a class="headerlink" href="#render-to-response" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">django.shortcuts.render_to_response</span></tt> renders a given template with a given
context dictionary and returns an <tt class="docutils literal"><span class="pre">HttpResponse</span></tt> object with that rendered
text.</p>
<div class="section" id="s-required-arguments">
<span id="required-arguments"></span><h3>Required arguments<a class="headerlink" href="#required-arguments" title="Permalink to this headline">¶</a></h3>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">template</span></tt></dt>
<dd>The full name of a template to use or sequence of template names.</dd>
</dl>
</div>
<div class="section" id="s-optional-arguments">
<span id="optional-arguments"></span><h3>Optional arguments<a class="headerlink" href="#optional-arguments" title="Permalink to this headline">¶</a></h3>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">dictionary</span></tt></dt>
<dd>A dictionary of values to add to the template context. By default, this
is an empty dictionary. If a value in the dictionary is callable, the
view will call it just before rendering the template.</dd>
<dt><tt class="docutils literal"><span class="pre">context_instance</span></tt></dt>
<dd><p class="first">The context instance to render the template with. By default, the template
will be rendered with a <tt class="docutils literal"><span class="pre">Context</span></tt> instance (filled with values from
<tt class="docutils literal"><span class="pre">dictionary</span></tt>). If you need to use <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/templates/api/#subclassing-context-requestcontext"><em>context processors</em></a>, render the template with a
<tt class="docutils literal"><span class="pre">RequestContext</span></tt> instance instead. Your code might look something like
this:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="k">return</span> <span class="n">render_to_response</span><span class="p">(</span><span class="s">'my_template.html'</span><span class="p">,</span>
                          <span class="n">my_data_dictionary</span><span class="p">,</span>
                          <span class="n">context_instance</span><span class="o">=</span><span class="n">RequestContext</span><span class="p">(</span><span class="n">request</span><span class="p">))</span>
</pre></div>
</div>
</dd>
</dl>
<p><tt class="docutils literal"><span class="pre">mimetype</span></tt></p>
<blockquote>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>The MIME type to use for the resulting document. Defaults to the value of
the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/settings/#setting-DEFAULT_CONTENT_TYPE"><tt class="xref docutils literal"><span class="pre">DEFAULT_CONTENT_TYPE</span></tt></a> setting.</p>
</blockquote>
</div>
<div class="section" id="s-example">
<span id="example"></span><h3>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h3>
<p>The following example renders the template <tt class="docutils literal"><span class="pre">myapp/index.html</span></tt> with the
MIME type <tt class="docutils literal"><span class="pre">application/xhtml+xml</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.shortcuts</span> <span class="k">import</span> <span class="n">render_to_response</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c"># View code here...</span>
    <span class="k">return</span> <span class="n">render_to_response</span><span class="p">(</span><span class="s">'myapp/index.html'</span><span class="p">,</span> <span class="p">{</span><span class="s">"foo"</span><span class="p">:</span> <span class="s">"bar"</span><span class="p">},</span>
        <span class="n">mimetype</span><span class="o">=</span><span class="s">"application/xhtml+xml"</span><span class="p">)</span>
</pre></div>
</div>
<p>This example is equivalent to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponse</span>
<span class="k">from</span> <span class="nn">django.template</span> <span class="k">import</span> <span class="n">Context</span><span class="p">,</span> <span class="n">loader</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c"># View code here...</span>
    <span class="n">t</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s">'myapp/template.html'</span><span class="p">)</span>
    <span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">({</span><span class="s">'foo'</span><span class="p">:</span> <span class="s">'bar'</span><span class="p">})</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">c</span><span class="p">),</span>
        <span class="n">mimetype</span><span class="o">=</span><span class="s">"application/xhtml+xml"</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-get-object-or-404">
<span id="get-object-or-404"></span><h2><tt class="docutils literal"><span class="pre">get_object_or_404</span></tt><a class="headerlink" href="#get-object-or-404" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">django.shortcuts.get_object_or_404</span></tt> calls
<tt class="xref docutils literal"><span class="pre">get()</span></tt> on a given model manager, but it raises
<tt class="docutils literal"><span class="pre">django.http.Http404</span></tt> instead of the model's <tt class="docutils literal"><span class="pre">DoesNotExist</span></tt> exception.</p>
<div class="section" id="s-id1">
<span id="id1"></span><h3>Required arguments<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">klass</span></tt></dt>
<dd>A <tt class="docutils literal"><span class="pre">Model</span></tt>, <tt class="docutils literal"><span class="pre">Manager</span></tt> or <tt class="docutils literal"><span class="pre">QuerySet</span></tt> instance from which to get the
object.</dd>
<dt><tt class="docutils literal"><span class="pre">**kwargs</span></tt></dt>
<dd>Lookup parameters, which should be in the format accepted by <tt class="docutils literal"><span class="pre">get()</span></tt> and
<tt class="docutils literal"><span class="pre">filter()</span></tt>.</dd>
</dl>
</div>
<div class="section" id="s-id2">
<span id="id2"></span><h3>Example<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
<p>The following example gets the object with the primary key of 1 from
<tt class="docutils literal"><span class="pre">MyModel</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.shortcuts</span> <span class="k">import</span> <span class="n">get_object_or_404</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">my_object</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">MyModel</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="mf">1</span><span class="p">)</span>
</pre></div>
</div>
<p>This example is equivalent to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">Http404</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">my_object</span> <span class="o">=</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="mf">1</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">Http404</span>
</pre></div>
</div>
<p>Note: As with <tt class="docutils literal"><span class="pre">get()</span></tt>, an <tt class="docutils literal"><span class="pre">MultipleObjectsReturned</span></tt> exception will be
raised if more than one object is found.</p>
</div>
</div>
<div class="section" id="s-get-list-or-404">
<span id="get-list-or-404"></span><h2><tt class="docutils literal"><span class="pre">get_list_or_404</span></tt><a class="headerlink" href="#get-list-or-404" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">django.shortcuts.get_list_or_404</span></tt> returns the result of
<tt class="xref docutils literal"><span class="pre">filter()</span></tt> on a given model manager, raising
<tt class="docutils literal"><span class="pre">django.http.Http404</span></tt> if the resulting list is empty.</p>
<div class="section" id="s-id3">
<span id="id3"></span><h3>Required arguments<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">klass</span></tt></dt>
<dd>A <tt class="docutils literal"><span class="pre">Model</span></tt>, <tt class="docutils literal"><span class="pre">Manager</span></tt> or <tt class="docutils literal"><span class="pre">QuerySet</span></tt> instance from which to get the
object.</dd>
<dt><tt class="docutils literal"><span class="pre">**kwargs</span></tt></dt>
<dd>Lookup parameters, which should be in the format accepted by <tt class="docutils literal"><span class="pre">get()</span></tt> and
<tt class="docutils literal"><span class="pre">filter()</span></tt>.</dd>
</dl>
</div>
<div class="section" id="s-id4">
<span id="id4"></span><h3>Example<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h3>
<p>The following example gets all published objects from <tt class="docutils literal"><span class="pre">MyModel</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.shortcuts</span> <span class="k">import</span> <span class="n">get_list_or_404</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">my_objects</span> <span class="o">=</span> <span class="n">get_list_or_404</span><span class="p">(</span><span class="n">MyModel</span><span class="p">,</span> <span class="n">published</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>This example is equivalent to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">Http404</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">my_objects</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">published</span><span class="o">=</span><span class="bp">True</span><span class="p">))</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">my_objects</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">Http404</span>
</pre></div>
</div>
</div>
</div>
</div>



<div id="content-secondary">
  <h2 id="comments">Questions/Feedback</h2>
  <p>Having trouble? We'd like to help!</p>
  <ul>
    <li>
      Try the <a href="http://docs.djangoproject.com/en/dev/faq/">FAQ</a> — it's got answers to many common
      questions.
    </li>
    <li>
      Search for information in the <a href="http://groups.google.com/group/django-users/">archives of the
      django-users mailing list</a>, or <a href="http://groups.google.com/group/django-users/">post a question</a>.
    </li>
    <li>
      Ask a question in the <a href="irc://irc.freenode.net/">#django IRC
      channel</a>, or search the <a href="http://oebfare.com/logger/django/">IRC
      logs</a> to see if its been asked before.
    </li>
    <li>
      If you notice errors with this documentation, please <a href="http://code.djangoproject.com/simpleticket?component=Documentation">
      open a ticket</a> and let us know! Please only use the ticket tracker for
      criticisms and improvements on the docs. For tech support, use the
      resources above.
    </li>
  </ul>
</div>

		</div>
		<!-- END #content-main -->
		<div id="content-related" class="sidebar">
		
  
    <h2>Contents</h2>
    
      <ul>
<li><a class="reference external" href="">Django shortcut functions</a><ul>
<li><a class="reference external" href="#render-to-response"><tt class="docutils literal"><span class="pre">render_to_response()</span></tt></a><ul>
<li><a class="reference external" href="#required-arguments">Required arguments</a></li>
<li><a class="reference external" href="#optional-arguments">Optional arguments</a></li>
<li><a class="reference external" href="#example">Example</a></li>
</ul>
</li>
<li><a class="reference external" href="#get-object-or-404"><tt class="docutils literal"><span class="pre">get_object_or_404</span></tt></a><ul>
<li><a class="reference external" href="#id1">Required arguments</a></li>
<li><a class="reference external" href="#id2">Example</a></li>
</ul>
</li>
<li><a class="reference external" href="#get-list-or-404"><tt class="docutils literal"><span class="pre">get_list_or_404</span></tt></a><ul>
<li><a class="reference external" href="#id3">Required arguments</a></li>
<li><a class="reference external" href="#id4">Example</a></li>
</ul>
</li>
</ul>
</li>
</ul>

    
  
  
  
    <h2>Search</h2>
    
    <form action="/en/1.0/search/" id="search" class="search">
  <div>
    <input name="cx" value="009763561546736975936:e88ek0eurf4" type="hidden">
    <input name="cof" value="FORID:11" type="hidden">
    <input name="ie" value="UTF-8" type="hidden">
    <input name="hl" value="" type="hidden">
    <input style="background: rgb(255, 255, 255) url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif) no-repeat scroll left center; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" id="id_search_q" class="query" name="q" type="text">
    <input name="sa" class="submit" value="Search" type="submit">
    <ul>
<li><label for="id_search_as_q_0"><input checked="checked" id="id_search_as_q_0" value="more:dev_docs" name="as_q" type="radio"> Latest</label></li>
<li><label for="id_search_as_q_1"><input id="id_search_as_q_1" value="more:1.0_docs" name="as_q" type="radio"> 1.0</label></li>
<li><label for="id_search_as_q_2"><input id="id_search_as_q_2" value="more:0.96_docs" name="as_q" type="radio"> 0.96</label></li>
<li><label for="id_search_as_q_3"><input id="id_search_as_q_3" value="more:all_docs" name="as_q" type="radio"> All</label></li>
</ul>
  </div>
</form>
<script type="text/javascript" src="Django%20%7C%20Django%20shortcut%20functions%20%7C%20Django%20Documentation_files/brand.html"></script>
  
  
  
    <h2>Browse</h2>
    <ul>
      
        
          <li>Prev: <a href="http://docs.djangoproject.com/en/1.0/topics/http/file-uploads/">File Uploads</a></li>
        
        
          <li>Next: <a href="http://docs.djangoproject.com/en/1.0/topics/http/generic-views/">Generic views</a></li>
        
        <li><a href="http://docs.djangoproject.com/en/1.0/contents/">Table of contents</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/genindex/">General Index</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/modindex/">Global Module Index</a></li>
        
      
    </ul>
  
  
  
    <h2>You are here:</h2>
    <ul>
      
        <li>
          <a href="http://docs.djangoproject.com/en/1.0/">Django 1.0 documentation</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/topics/">Using Django</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/topics/http/">Handling HTTP requests</a>
          
          <ul><li>Django shortcut functions</li></ul>
          </li></ul></li></ul>
        </li>
      
    </ul>
  
  
  
    <h3>Last update:</h3>
    <div>May 23, 2009, 8:15 a.m. (<a href="http://www.timeanddate.com/worldclock/city.html?n=64">CDT</a>)</div>
  

		</div>
		<!-- END #content-related -->

		</div>
		<!-- END #content -->
		<div id="footer">
			<p>© 2005-2009 <a href="http://www.djangoproject.com/foundation/">Django Software Foundation</a> unless otherwise noted. Django is a registered trademark of the Django Software Foundation. 
			Hosting graciously provided by <a href="http://mediatemple.net/">
			<img style="vertical-align: middle; position: relative; top: -1px;" src="Django%20%7C%20Django%20shortcut%20functions%20%7C%20Django%20Documentation_files/mt.png" alt="media temple"></a>
			</p>
		</div>
		<!-- END #footer -->
	</div>
	<!-- END #container -->
	</body></html>